package com.example.newsdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Switch;

import com.example.newsdemo.fragment.CinemaFragment;
import com.example.newsdemo.fragment.HomeFragment;
import com.example.newsdemo.fragment.MineFragment;
import com.example.newsdemo.fragment.VideoFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<Fragment> fragmentList=new ArrayList<Fragment>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiView();
    }
    private void intiView(){
        BottomNavigationView dnv=findViewById(R.id.home_bottom);
        fragmentList.add(new HomeFragment());
        fragmentList.add(new VideoFragment());
        fragmentList.add(new CinemaFragment());
        fragmentList.add(new MineFragment());

        FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
        for (int i=0;i<fragmentList.size();i++){
            ft.add(R.id.home_fragment_container,fragmentList.get(i));
            ft.hide(fragmentList.get(i));
            if(i==0){
                ft.show(fragmentList.get(0));
            }
        }
        ft.commit();
        dnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                showSelectFrafment(item.getItemId());
                return true;
                }
        });
    }
    private void showSelectFrafment(int menuId){
        FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
        for (int i=0;i<fragmentList.size();i++){
            ft.hide(fragmentList.get(i));
            switch (menuId){
                case  R.id.home_item_home:
                    ft.show(fragmentList.get(0));
                    break;

                case  R.id.home_item_play:
                    ft.show(fragmentList.get(1));
                    break;

                case R.id.home_item_video:
                    ft.show(fragmentList.get(2));
                    break;

                case R.id.home_item_me:
                    ft.show(fragmentList.get(3));
                    break;

            }
        }
        ft.commit();
    }
}